package jmind.core.pdf;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.*;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * pdf工具类
 * https://www.atatech.org/articles/85498?spm=ata.13269325.0.0.37c449fa2Qs2aQ
 * https://blog.csdn.net/TOP__ONE/article/details/65442390
 * @author weibo
 * @date 2019/11/12
 */
public class PdfUtil {

    /**
     * 根据PDF模板生成新的PDF
     * @param map  模板表单域的属性和值
     * @param sourceFile  模板文件
     * @param targetFile  新的pdf文件
     * @return
     */
    public static  File createByPdfTemplate(Map<String,String> map,String sourceFile,String targetFile){
        File file=new File(targetFile);
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            // 输出流
            out = new FileOutputStream(file);
            // 读取pdf模板
            reader = new PdfReader(sourceFile);
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();

            Map<String, AcroFields.Item> fields = form.getFields();
            fields.forEach((k,v)->{
                String val=map.get(k);
                if(val!=null){
                    try {
                        form.setField(k,val);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (DocumentException e) {
                        e.printStackTrace();
                    }

                }
            });
            // 如果为false那么生成的PDF文件还能编辑，一定要设为true
            stamper.setFormFlattening(true);
            stamper.close();
            PdfImportedPage importPage = null;
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();

            ///循环是处理成品只显示一页的问题
            for (int i=1;i<=reader.getNumberOfPages();i++){
                importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
                copy.addPage(importPage);
            }
            doc.close();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        return  file ;

    }

    public static void main(String[] args) {
        Map<String,String>  map=new HashMap<>();
        map.put("name","企业名称");
        map.put("address","企业地址");
        map.put("concat","韦搏谢");
        map.put("email","weib@163.com");
        map.put("mobile","13710321837");
        String path="/Users/weibo/Downloads/" ;
      //  createByPdfTemplate(map,path+"0814-生产企业.pdf",path+"out2.pdf");


        map.put("fill_1","xxx1");
        map.put("fill_2","xxx2");
        map.put("fill_3","xxx3");
        map.put("fill_4","xxx4");

        map.put("abc","xxx12");
        map.put("def","xxx31");
        map.put("hj","xxx14");
        createByPdfTemplate(map,path+"xx.pdf",path+"out2xx.pdf");

    }
}
